package com.irdstudio.apicenter.frame.web;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.irdstudio.apicenter.frame.core.dm.SWebAction;
import com.irdstudio.apicenter.frame.core.dm.SWebActionDao;
import com.irdstudio.apicenter.frame.web.controller.CreateController;
import com.irdstudio.apicenter.frame.web.controller.RetrieveController;
import com.irdstudio.apicenter.frame.web.controller.UpdateController;

/**
 * 通讯控制总线辅助类（读取总线下交易、通道、报文要素）
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2015-05-20
 */
public class MacmsControllerBusUtil {

	/* 文本日志记录对象(Logback对象) */
	private final static Logger logger = LoggerFactory
			.getLogger(MacmsControllerBusUtil.class);

	/**
	 * 读取所有Web交易配置信息到控制总线类中
	 * 
	 * @param conn
	 * @return
	 * @throws MacmsException
	 */
	public static MacmsControllerBus initializeBusConfigure(Connection conn)
			throws MacmsException {

		MacmsControllerBus ctrlBus = new MacmsControllerBus();
		SWebActionDao swadDao = new SWebActionDao(conn);

		try {

			logger.info("读取Web交易配置信息...");

			// 读取所有Web交易配置信息进入内存
			List<SWebAction> swaList = swadDao.queryWithCond("", "");

			logger.info("初始化新增类Web交易控制器...");
			ctrlBus.addWebController(MacmsWebConstants.CONTROLLER_SC,
					new CreateController());
			logger.info("读取新增类Web交易配置信息...");
			for (SWebAction swa : swaList) {
				if (MacmsWebConstants.CONTROLLER_SC.equals(swa.getActionType())) {
					ctrlBus.addWebAction(swa.getActionId(), swa);
					logger.info("--Add Web Action:" + swa.getActionId() + ","
							+ swa.getActionDesc() + "...");
				}
			}

			logger.info("初始化修改类Web交易控制器...");
			ctrlBus.addWebController(MacmsWebConstants.CONTROLLER_SU,
					new UpdateController());
			logger.info("读取修改类Web交易配置信息...");
			for (SWebAction swa : swaList) {
				if (MacmsWebConstants.CONTROLLER_SU.equals(swa.getActionType())) {
					ctrlBus.addWebAction(swa.getActionId(), swa);
					logger.info("--Add Web Action:" + swa.getActionId() + ","
							+ swa.getActionDesc() + "...");
				}
			}

			logger.info("初始化查看类Web交易控制器...");
			ctrlBus.addWebController(MacmsWebConstants.CONTROLLER_SR,
					new RetrieveController());
			logger.info("读取查看类Web交易配置信息...");
			for (SWebAction swa : swaList) {
				if (MacmsWebConstants.CONTROLLER_SR.equals(swa.getActionType())) {
					ctrlBus.addWebAction(swa.getActionId(), swa);
					logger.info("--Add Web Action:" + swa.getActionId() + ","
							+ swa.getActionDesc() + "...");
				}
			}

			ctrlBus.setReady(true);

		} catch (SQLException e) {
			e.printStackTrace();
			ctrlBus.setReady(false);
			throw new MacmsException("加载Web交易配置信息失败!");
		}

		return ctrlBus;
	}

}
